﻿using System;

namespace Domain.Infrastructure
{

    /// <summary>
    /// 实体 基础类
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    // ReSharper disable once UnusedTypeParameter 
    public class Entity<TEntity> where TEntity : class
    { 
        ///// <summary>
        ///// 操作员登陆ID
        ///// </summary>
        //public string OperatorLoginId { get; set; }

        /// <summary>
        /// 创建
        /// </summary>
        public void Create(string userId)
        {
            //创建约束处理
            if (this is ICreationAudited entity)
            {
                entity.Id = Common.GuId();
                entity.CreatorTime = DateTime.Now; 
                entity.CreatorUserId = userId;

            }
            //删除约束处理
            if (this is IDeleteAudited entityDelete)
            {
                entityDelete.DeleteMark = false;
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="keyValue"></param>
        public void Modify(string userId, string keyValue)
        {

            var entity = this as IModifyAudited;
            if (entity == null) return;
            entity.Id = keyValue;
            entity.LastModifyTime = DateTime.Now;
            entity.LastModifyUserId = userId;

        }



        /// <summary>
        /// 逻辑删除
        /// </summary>
        public void Remove(string userId)
        {
            var entity = this as IDeleteAudited;
            if (entity == null) return;
            entity.DeleteTime = DateTime.Now;
            entity.DeleteMark = true;
            entity.DeleteUserId = userId;

        }
    }
}
